let header = {
  'Content-Type': "application/json",
}
/**
 * 供外部post请求调用  
 */
let post = (url, params, onSuccess, onFailed) => {
  // console.log("请求方式：", "POST")
  request(url, params, "POST", onSuccess, onFailed);
}
/**
 * 供外部get请求调用
 */
let get = (url, params, onSuccess, onFailed) => {
  // console.log("请求方式：", "GET")
  request(url, params, "GET", onSuccess, onFailed);
}

/**
 * function: 封装网络请求
 * @url URL地址
 * @params 请求参数
 * @method 请求方式：GET/POST
 * @onSuccess 成功回调
 * @onFailed  失败回调
 */

let request = (url, params, method, onSuccess, onFailed) => {
  // console.log('请求url：' + url);
  wx.showLoading({
    title: "正在加载中...",
  })
  wx.request({
    url: url,
    data: dealParams(params),
    method: method,
    success: function (res) {
      wx.hideLoading();
      if (res.data) {
        if (res.data.message == '成功') {
          onSuccess(res);
        } else {
          onFailed(res);
        }
      }
    },
    fail: function (error) {
      onFailed("");
    }
  })
}
/**
 * function: 根据需求处理请求参数：添加固定参数配置等
 * @params 请求参数
 */
let dealParams = (params) => {
  // console.log("请求参数:", params)
  return params;
}
// 1.通过module.exports方式提供给外部调用

export default {
  post,
  get
}